package com.wyj.service.comment;

import com.wyj.dao.CommentMapper;
import com.wyj.pojo.Blog;
import com.wyj.pojo.Comment;
import com.wyj.service.blog.BlogService;
import com.wyj.util.OutInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private BlogService blogService;

    // 临时子集的集合
    private List<Comment> childComment = new ArrayList<>();

    @Override
    public List<Comment> queryCommentsByBlogId(Long blogId) {
        return commentMapper.queryCommentsByBlogId(blogId);
    }

    @Override
    public Comment queryCommentById(Long id) {
        return commentMapper.queryCommentById(id);
    }


    // 所有的顶级
    @Override
    public List<Comment> queryTopCommentsByBlogId(Long blogId) {
        // 获取所有顶级
        List<Comment> commentList = commentMapper.queryTopCommentsByBlogId(blogId);
        allChildComments(commentList);
        return commentList;
    }

    // 获取每个顶级的所有子集
    private void allChildComments(List<Comment> commentList){
        // 得到每一个顶级
        for (Comment comment : commentList) {
            // 一个顶级所有的一级的回复
            List<Comment> comments = queryChildComments(comment.getId());
            // 顶级的所有一级回复 放入 replyComments
            comment.setReplyComments(comments);
            // 遍历出当前顶级的所有子集
            iterationComment(comment);
            // 更新顶级的回复为所有回复
            comment.setReplyComments(childComment);
            // 清除临时子集区
            childComment = new ArrayList<>();
        }
    }

    @Override
    public List<Comment> queryChildComments(Long id) {
        return commentMapper.queryChildComments(id);
    }

    // 迭代
    public void iterationComment(Comment comment){
        if(comment.getReplyComments().size() > 0){
            // 得到一级回复
            List<Comment> replyComments = comment.getReplyComments();
            for (Comment reply : replyComments) {
                if (reply.getParentCommentId() != null){
                    reply.setParentComment(commentMapper.queryCommentById(reply.getParentCommentId()));
                }
                childComment.add(reply);
                reply.setReplyComments(queryChildComments(reply.getId()));
                if (reply.getReplyComments().size() > 0){
                    iterationComment(reply);
                }
            }
        }
    }


    @Transactional
    @Override
    public int saveComment(Comment comment) {

        // 增加博客评论次数
        blogService.updateBlogCommentCount(comment.getBlogId(), blogService.queryBlogCommentCount(comment.getBlogId()) + 1);

        Long id = comment.getParentComment().getId();
        if (id != -1){
            comment.setParentCommentId(id);
        }else {
            comment.setParentCommentId(null);
        }
        comment.setCreateTime(new Date());
        System.out.println(" ======> " + comment.getBlogId());



        return commentMapper.saveComment(comment);
    }
}
